package principal;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.ObjectInputStream.GetField;

import javax.swing.JButton;

import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.JTextPane;

import estrutura.Celula;
import estrutura.Peca;

import peca.CorPeca;

import util.PecaService;
import util.Relatorio;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Console extends javax.swing.JPanel implements KeyListener, ActionListener{
	private JTextPane jTextPane1;
	private JButton jButtonRelatorio;
	private static Tabuleiro tabuleiro;
	private CorPeca cor;

	static Celula[][] celulas;
	
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	 * @param branca 
	*/
	//public static void main(String[] args) {
	public static void inicio(Tabuleiro tabuleiro, CorPeca cor) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new Console(tabuleiro,cor));
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setTitle("Console "+cor.name());
		frame.setVisible(true);
	}
	
	public Console(Tabuleiro tabuleiro, CorPeca cor) {
		this.setTabuleiro(tabuleiro);
		this.cor = cor;
		initGUI();
	}
	
	public Console(Tabuleiro tabuleiro, CorPeca cor,boolean gui) {
		this.setTabuleiro(tabuleiro);
		this.cor = cor;
	}

	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(398, 129));
			this.setLayout(null);
			{
				jTextPane1 = new JTextPane();
				this.add(jTextPane1);
				jTextPane1.setBounds(0, 0, 112, 38);
				jTextPane1.setFont(new java.awt.Font("Segoe UI",0,20));
				jTextPane1.addKeyListener(this);
			}
			{
				jButtonRelatorio = new JButton();
				this.add(jButtonRelatorio);
				jButtonRelatorio.setText("Relatorio Jogadas");
				jButtonRelatorio.setBounds(124, 1, 215, 38);
				jButtonRelatorio.addActionListener(this);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyChar() == KeyEvent.VK_ENTER){
			System.out.println(jTextPane1.getText());
			if(jTextPane1.getText().length() == 5){	
				jogar(jTextPane1.getText());
				jTextPane1.setText("s");
				e.setKeyCode(KeyEvent.VK_BACK_SPACE);
			}
			
		}
		
	}

	
	
	public static void jogar(final String jogada) {
		final String[] movimentos = jogada.split(" ");
			 
			new Thread(){
				public void run(){
					getTabuleiro().jogar(PecaService.getCelula(movimentos[0], getTabuleiro().getComponents()));
					
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
					
					 getTabuleiro().jogar(PecaService.getCelula(movimentos[1], getTabuleiro().getComponents()));
				}
			}.start();
		}
	
	
	public Celula[][] jogarSimulacao(final String jogada) {
		final String[] movimentos = jogada.split(" ");
		getTabuleiro().jogar(PecaService.getCelula(movimentos[0], getTabuleiro().getComponents()));		
		 celulas = getTabuleiro().jogar(PecaService.getCelula(movimentos[1], getTabuleiro().getComponents()));
		return celulas;
		
	}
	
	

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		//Relatorio.relatorio(cor,tabuleiro.getCelulas());
		jogar(Relatorio.simulaJogada(cor,getTabuleiro().getCelulas()));
	}
	
	public static void maquinaJogar(CorPeca cor, Tabuleiro tabuleiro) {
	
		final Console console = new Console(tabuleiro, cor, false);
					
				String jogada = Simulador.retornaMelhor(console.tabuleiro, console.cor);
				
				jogar(jogada);

	}

	public static void setTabuleiro(Tabuleiro tabuleiro) {
		Console.tabuleiro = tabuleiro;
	}

	public static Tabuleiro getTabuleiro() {
		return tabuleiro;
	}

}
